﻿/************************************************************************************
* Copyright (c) 2019 All Rights Reserved.
*命名空间：Separate.BLL
*文件名： Parameter
*创建人： Liebeskind
*创建时间：2019/6/17 16:02:51
*描述
*=====================================================================
*修改标记
*修改时间：2019/6/17 16:02:51
*修改人：Liebeskind
*描述：
************************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Commonality.Model.Xml;
using Commonality.Model.Tray;
using Commonality.DAL.MySQL;
using Separate.Model.EmptyTrayGather;

namespace Separate.BLL
{
    public class Parameter
    {
        public static string parameterFeilName = Application.StartupPath + @"\Parameter\Parameter.xml";

        #region 空Tray收集型号仓位映射参数
        /// <summary>
        /// 加载空Tray收集仓位映射参数
        /// </summary>
        public static void EmptyTrayGatherParameterLoad()
        {
            try
            {
                EmptyTrayMap.Storehouse1 = (TrayModel)Convert.ToInt16(XmlOperater.ReadItemFromXml(parameterFeilName, "IPC", "EmptyTrayMap", "Storehouse1"));
                EmptyTrayMap.Storehouse2 = (TrayModel)Convert.ToInt16(XmlOperater.ReadItemFromXml(parameterFeilName, "IPC", "EmptyTrayMap", "Storehouse2"));
                EmptyTrayMap.Storehouse3 = (TrayModel)Convert.ToInt16(XmlOperater.ReadItemFromXml(parameterFeilName, "IPC", "EmptyTrayMap", "Storehouse3"));
                EmptyTrayMap.Storehouse4 = (TrayModel)Convert.ToInt16(XmlOperater.ReadItemFromXml(parameterFeilName, "IPC", "EmptyTrayMap", "Storehouse4"));
                EmptyTrayMap.Storehouse5 = (TrayModel)Convert.ToInt16(XmlOperater.ReadItemFromXml(parameterFeilName, "IPC", "EmptyTrayMap", "Storehouse5"));
                EmptyTrayMap.Storehouse6 = (TrayModel)Convert.ToInt16(XmlOperater.ReadItemFromXml(parameterFeilName, "IPC", "EmptyTrayMap", "Storehouse6"));
            }
            catch (Exception)
            {
            }

        }
        /// <summary>
        /// 空Tray参数映射设置
        /// </summary>
        /// <param name="storehouse">映射仓位</param>
        /// <param name="trayModel">映射类型</param>
        public static void EmptyTrayGatherParameterMap(short storehouse,TrayModel trayModel)
        {
            switch (storehouse)
            {
                case 1:
                    EmptyTrayMap.Storehouse1 = trayModel;
                    XmlOperater.WriteItemToXml(parameterFeilName, "IPC", "EmptyTrayMap", "Storehouse1", ((int)trayModel).ToString());
                    break;
                case 2:
                    EmptyTrayMap.Storehouse2 = trayModel;
                    XmlOperater.WriteItemToXml(parameterFeilName, "IPC", "EmptyTrayMap", "Storehouse2", ((int)trayModel).ToString());
                    break;
                case 3:
                    EmptyTrayMap.Storehouse3 = trayModel;
                    XmlOperater.WriteItemToXml(parameterFeilName, "IPC", "EmptyTrayMap", "Storehouse3", ((int)trayModel).ToString());
                    break;
                case 4:
                    EmptyTrayMap.Storehouse4 = trayModel;
                    XmlOperater.WriteItemToXml(parameterFeilName, "IPC", "EmptyTrayMap", "Storehouse4", ((int)trayModel).ToString());
                    break;
                case 5:
                    EmptyTrayMap.Storehouse5 = trayModel;
                    XmlOperater.WriteItemToXml(parameterFeilName, "IPC", "EmptyTrayMap", "Storehouse5", ((int)trayModel).ToString());
                    break;
                case 6:
                    EmptyTrayMap.Storehouse6 = trayModel;
                    XmlOperater.WriteItemToXml(parameterFeilName, "IPC", "EmptyTrayMap", "Storehouse6", ((int)trayModel).ToString());
                    break;
                default:
                    break;
            }
        }
        #endregion

        #region 数据库
        public static void MySQLParameterLoad()
        {
            try
            {
                MySqlFunction.InitMySQLFunction();
                MySQLParameter.Host = XmlOperater.ReadItemFromXml(parameterFeilName, "IPC", "MySQL", "Host");
                MySQLParameter.Password = XmlOperater.ReadItemFromXml(parameterFeilName, "IPC", "MySQL", "Password");
                MySQLParameter.UserID = XmlOperater.ReadItemFromXml(parameterFeilName, "IPC", "MySQL", "UserID");
                MySqlFunction.MySql.DataSource = MySQLParameter.Host;
                MySqlFunction.MySql.Password = MySQLParameter.Password;
                MySqlFunction.MySql.UserId = MySQLParameter.UserID;
            }
            catch (Exception)
            {
            }
        }
        public static void UpdateMySQLHost(string host)
        {
            XmlOperater.WriteItemToXml(parameterFeilName, "IPC", "MySQL", "Host", host);
            MySqlFunction.MySql.DataSource = host;
        }
        public static void UpdateMySQLPassword(string password)
        {
            XmlOperater.WriteItemToXml(parameterFeilName, "IPC", "MySQL", "Password", password);
            MySqlFunction.MySql.Password = password;
        }
        public static void UpdateMySQLUserId(string userId)
        {
            XmlOperater.WriteItemToXml(parameterFeilName, "IPC", "MySQL", "UserID", userId);
            MySqlFunction.MySql.UserId = userId;
        }
        #endregion
        #region 分盘类型记录
        /// <summary>
        /// 设备运行中缓存位和分盘位当前产品类型加载
        /// </summary>
        public static void EmptyTrayLoad()
        {
            try
            {
                EmptyTray.AEmptyTrayModel_BUFF = (TrayModel)(Convert.ToInt32( XmlOperater.ReadItemFromXml(parameterFeilName, "IPC", "EmptyTray", "AEmptyTrayModel_BUFF")));
                EmptyTray.BEmptyTrayModel_BUFF = (TrayModel)(Convert.ToInt32(XmlOperater.ReadItemFromXml(parameterFeilName, "IPC", "EmptyTray", "BEmptyTrayModel_BUFF")));
                //AutoForm.autoForm.buffDisA(System.Drawing.Color.Lime, EmptyTray.AEmptyTrayModel_BUFF.ToString(), "");
                //AutoForm.autoForm.buffDisB(System.Drawing.Color.Lime, EmptyTray.BEmptyTrayModel_BUFF.ToString(), "");

                EmptyTray.AEmptyTrayModel = (TrayModel)(Convert.ToInt32(XmlOperater.ReadItemFromXml(parameterFeilName, "IPC", "EmptyTray", "AEmptyTrayModel")));
                EmptyTray.BEmptyTrayModel = (TrayModel)(Convert.ToInt32(XmlOperater.ReadItemFromXml(parameterFeilName, "IPC", "EmptyTray", "BEmptyTrayModel")));
            }
            catch (Exception)
            {

            }
        }
        /// <summary>
        /// 设备运行中缓存位当前产品类型记录
        /// </summary>
        public static void writeAEmptyTrayModel_BUFF(ref TrayModel trayModel)
        {
            XmlOperater.WriteItemToXml(parameterFeilName, "IPC", "EmptyTray", "AEmptyTrayModel_BUFF", ((int)trayModel).ToString());
        }
        /// <summary>
        /// 设备运行中缓存位当前产品类型记录
        /// </summary>
        public static void writeBEmptyTrayModel_BUFF(ref TrayModel trayModel)
        {
            XmlOperater.WriteItemToXml(parameterFeilName, "IPC", "EmptyTray", "BEmptyTrayModel_BUFF", ((int)trayModel).ToString());
        }
        /// <summary>
        /// 设备运行中分盘位当前产品类型记录
        /// </summary>
        public static void writeAEmptyTrayModel(ref TrayModel trayModel)
        {
            XmlOperater.WriteItemToXml(parameterFeilName, "IPC", "EmptyTray", "AEmptyTrayModel", ((int)trayModel).ToString());
        }
        /// <summary>
        /// 设备运行中分盘位当前产品类型记录
        /// </summary>
        public static void writeBEmptyTrayModel(ref TrayModel trayModel)
        {
            XmlOperater.WriteItemToXml(parameterFeilName, "IPC", "EmptyTray", "BEmptyTrayModel", ((int)trayModel).ToString());
        }
        #endregion
        #region Model类型集合元素
        public static void TrayModelElementLoad()
        {
            try
            {
                string[] modelX01 = XmlOperater.ReadItemFromXml(parameterFeilName, "IPC", "TrayModelSets", "X01").Split(new char[] { '|' });
                string[] modelX03 = XmlOperater.ReadItemFromXml(parameterFeilName, "IPC", "TrayModelSets", "X03").Split(new char[] { '|' });
                string[] modelX05 = XmlOperater.ReadItemFromXml(parameterFeilName, "IPC", "TrayModelSets", "X05").Split(new char[] { '|' });
                string[] modelX08 = XmlOperater.ReadItemFromXml(parameterFeilName, "IPC", "TrayModelSets", "X08").Split(new char[] { '|' });
                string[] modelX13 = XmlOperater.ReadItemFromXml(parameterFeilName, "IPC", "TrayModelSets", "X13").Split(new char[] { '|' });
                string[] modelX15 = XmlOperater.ReadItemFromXml(parameterFeilName, "IPC", "TrayModelSets", "X15").Split(new char[] { '|' });
                foreach (var item in modelX01)
                {
                    if(item != "" && item != null) Tray.X01.Add(item);
                }
                foreach (var item in modelX03)
                {
                    if (item != "" && item != null) Tray.X03.Add(item);
                }
                foreach (var item in modelX05)
                {
                    if (item != "" && item != null) Tray.X05.Add(item);
                }
                foreach (var item in modelX08)
                {
                    if (item != "" && item != null) Tray.X08.Add(item);
                }
                foreach (var item in modelX13)
                {
                    if (item != "" && item != null) Tray.X13.Add(item);
                }
                foreach (var item in modelX15)
                {
                    if (item != "" && item != null) Tray.X15.Add(item);
                }
            }
            catch (Exception)
            {
            }
        }
        public static void UpdateTrayModelElement(TrayModel model, List<string> modelSets)
        {
            string Sets = "";
            foreach (var item in modelSets)
            {
                Sets += item + "|";
            }
            switch (model)
            {
                case TrayModel.NULL:
                    break;
                case TrayModel.X01:
                    XmlOperater.WriteItemToXml(parameterFeilName, "IPC", "TrayModelSets", "X01", Sets);
                    break;
                case TrayModel.X03:
                    XmlOperater.WriteItemToXml(parameterFeilName, "IPC", "TrayModelSets", "X03", Sets);
                    break;
                case TrayModel.X05:
                    XmlOperater.WriteItemToXml(parameterFeilName, "IPC", "TrayModelSets", "X05", Sets);
                    break;
                case TrayModel.X08:
                    XmlOperater.WriteItemToXml(parameterFeilName, "IPC", "TrayModelSets", "X08", Sets);
                    break;
                case TrayModel.X13:
                    XmlOperater.WriteItemToXml(parameterFeilName, "IPC", "TrayModelSets", "X13", Sets);
                    break;
                case TrayModel.X15:
                    XmlOperater.WriteItemToXml(parameterFeilName, "IPC", "TrayModelSets", "X15", Sets);
                    break;
                default:
                    break;
            }
        }
        #endregion
        #region 标记线别及料号
        public static void ExcludeLineLoad()
        {
            try
            {
                ExcludeList.ExcludeLine.TB1_1FT_1 = Convert.ToByte(XmlOperater.ReadItemFromXml(parameterFeilName, "IPC", "ExcludeList", "Line", "L1"));
                ExcludeList.ExcludeLine.TB1_1FT_2 = Convert.ToByte(XmlOperater.ReadItemFromXml(parameterFeilName, "IPC", "ExcludeList", "Line", "L2"));
                ExcludeList.ExcludeLine.TB1_1FT_3 = Convert.ToByte(XmlOperater.ReadItemFromXml(parameterFeilName, "IPC", "ExcludeList", "Line", "L3"));
                ExcludeList.ExcludeLine.TB1_1FT_4 = Convert.ToByte(XmlOperater.ReadItemFromXml(parameterFeilName, "IPC", "ExcludeList", "Line", "L4"));
                ExcludeList.ExcludeLine.TB1_1FT_5 = Convert.ToByte(XmlOperater.ReadItemFromXml(parameterFeilName, "IPC", "ExcludeList", "Line", "L5"));
                ExcludeList.ExcludeLine.TB1_1FT_6 = Convert.ToByte(XmlOperater.ReadItemFromXml(parameterFeilName, "IPC", "ExcludeList", "Line", "L6"));
                ExcludeList.ExcludeLine.TB1_1FT_7 = Convert.ToByte(XmlOperater.ReadItemFromXml(parameterFeilName, "IPC", "ExcludeList", "Line", "L7"));
                ExcludeList.ExcludeLine.TB1_1FT_8 = Convert.ToByte(XmlOperater.ReadItemFromXml(parameterFeilName, "IPC", "ExcludeList", "Line", "L8"));
                ExcludeList.ExcludeLine.TB1_1FT_9 = Convert.ToByte(XmlOperater.ReadItemFromXml(parameterFeilName, "IPC", "ExcludeList", "Line", "L9"));
                ExcludeList.ExcludeLine.TB1_1FT_10 = Convert.ToByte(XmlOperater.ReadItemFromXml(parameterFeilName, "IPC", "ExcludeList", "Line", "L10"));
                ExcludeList.ExcludeLine.TB1_1FT_11 = Convert.ToByte(XmlOperater.ReadItemFromXml(parameterFeilName, "IPC", "ExcludeList", "Line", "L11"));
                ExcludeList.ExcludeLine.TB1_1FT_12 = Convert.ToByte(XmlOperater.ReadItemFromXml(parameterFeilName, "IPC", "ExcludeList", "Line", "L12"));
                ExcludeList.ExcludeLine.TB1_1FT_13 = Convert.ToByte(XmlOperater.ReadItemFromXml(parameterFeilName, "IPC", "ExcludeList", "Line", "L13"));
                ExcludeList.ExcludeLine.TB1_1FT_14 = Convert.ToByte(XmlOperater.ReadItemFromXml(parameterFeilName, "IPC", "ExcludeList", "Line", "L14"));
                ExcludeList.ExcludeLine.TB1_1FT_15 = Convert.ToByte(XmlOperater.ReadItemFromXml(parameterFeilName, "IPC", "ExcludeList", "Line", "L15"));
                ExcludeList.ExcludeLine.TB1_1FT_16 = Convert.ToByte(XmlOperater.ReadItemFromXml(parameterFeilName, "IPC", "ExcludeList", "Line", "L16"));
            }
            catch (Exception)
            {

            }

        }
        public static void UpdateExcludeLine(short line,byte on)
        {
            switch (line)
            {
                case 1:
                    XmlOperater.WriteItemToXml(parameterFeilName, "IPC", "ExcludeList", "Line", "L1", on.ToString());
                    ExcludeList.ExcludeLine.TB1_1FT_1 = on;
                    break;
                case 2:
                    XmlOperater.WriteItemToXml(parameterFeilName, "IPC", "ExcludeList", "Line", "L2", on.ToString());
                    ExcludeList.ExcludeLine.TB1_1FT_2 = on;
                    break;
                case 3:
                    XmlOperater.WriteItemToXml(parameterFeilName, "IPC", "ExcludeList", "Line", "L3", on.ToString());
                    ExcludeList.ExcludeLine.TB1_1FT_3 = on;
                    break;
                case 4:
                    XmlOperater.WriteItemToXml(parameterFeilName, "IPC", "ExcludeList", "Line", "L4", on.ToString());
                    ExcludeList.ExcludeLine.TB1_1FT_4 = on;
                    break;
                case 5:
                    XmlOperater.WriteItemToXml(parameterFeilName, "IPC", "ExcludeList", "Line", "L5", on.ToString());
                    ExcludeList.ExcludeLine.TB1_1FT_5 = on;
                    break;
                case 6:
                    XmlOperater.WriteItemToXml(parameterFeilName, "IPC", "ExcludeList", "Line", "L6", on.ToString());
                    ExcludeList.ExcludeLine.TB1_1FT_6 = on;
                    break;
                case 7:
                    XmlOperater.WriteItemToXml(parameterFeilName, "IPC", "ExcludeList", "Line", "L7", on.ToString());
                    ExcludeList.ExcludeLine.TB1_1FT_7 = on;
                    break;
                case 8:
                    XmlOperater.WriteItemToXml(parameterFeilName, "IPC", "ExcludeList", "Line", "L8", on.ToString());
                    ExcludeList.ExcludeLine.TB1_1FT_8 = on;
                    break;
                case 9:
                    XmlOperater.WriteItemToXml(parameterFeilName, "IPC", "ExcludeList", "Line", "L9", on.ToString());
                    ExcludeList.ExcludeLine.TB1_1FT_9 = on;
                    break;
                case 10:
                    XmlOperater.WriteItemToXml(parameterFeilName, "IPC", "ExcludeList", "Line", "L10", on.ToString());
                    ExcludeList.ExcludeLine.TB1_1FT_10 = on;
                    break;
                case 11:
                    XmlOperater.WriteItemToXml(parameterFeilName, "IPC", "ExcludeList", "Line", "L11", on.ToString());
                    ExcludeList.ExcludeLine.TB1_1FT_11 = on;
                    break;
                case 12:
                    XmlOperater.WriteItemToXml(parameterFeilName, "IPC", "ExcludeList", "Line", "L12", on.ToString());
                    ExcludeList.ExcludeLine.TB1_1FT_12 = on;
                    break;
                case 13:
                    XmlOperater.WriteItemToXml(parameterFeilName, "IPC", "ExcludeList", "Line", "L13", on.ToString());
                    ExcludeList.ExcludeLine.TB1_1FT_13 = on;
                    break;
                case 14:
                    XmlOperater.WriteItemToXml(parameterFeilName, "IPC", "ExcludeList", "Line", "L14", on.ToString());
                    ExcludeList.ExcludeLine.TB1_1FT_14 = on;
                    break;
                case 15:
                    XmlOperater.WriteItemToXml(parameterFeilName, "IPC", "ExcludeList", "Line", "L15", on.ToString());
                    ExcludeList.ExcludeLine.TB1_1FT_15 = on;
                    break;
                case 16:
                    XmlOperater.WriteItemToXml(parameterFeilName, "IPC", "ExcludeList", "Line", "L16", on.ToString());
                    ExcludeList.ExcludeLine.TB1_1FT_16 = on;
                    break;
                default:
                    break;
            }
        }

        public static void ExcludePnLoad()
        {
            ExcludeList.ExcludePn = new List<string>();
            try
            {
                string[] PN = XmlOperater.ReadItemFromXml(parameterFeilName, "IPC", "ExcludeList", "PN", "PN").Split(new char[] { '|' });
                foreach (var item in PN)
                {
                    if (item != "" && item != null) ExcludeList.ExcludePn.Add(item);
                }
            }
            catch (Exception)
            {
            }
        }
        public static void UpdateExcludePn(List<string> PnList)
        {
            string PNi = "";
            foreach (var item in PnList)
            {
                PNi += item + "|";
            }
            XmlOperater.WriteItemToXml(parameterFeilName, "IPC", "ExcludeList", "PN", "PN", PNi);
        }
        #endregion
    }
}
